package com.mktech.taralib.http;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageCache;

public class VolleyHelper
{
	private static VolleyHelper instance = null;
	private RequestQueue mQueue;
	private ImageLoader mImgLoader;

	private VolleyHelper(Context cx)
	{
		mQueue = Volley.newRequestQueue(cx);
		mImgLoader = new ImageLoader(mQueue, new ImageCache()
		{

			private final LruCache<String, Bitmap> mLruCache = new LruCache<String, Bitmap>((int) (Runtime.getRuntime().maxMemory() / 10))
			{
				protected int sizeOf(String key, Bitmap value)
				{
					return value.getRowBytes() * value.getHeight();

				};
			};

			@Override
			public Bitmap getBitmap(String url)
			{
				// TODO Auto-generated method stub
				return mLruCache.get(url);
			}

			@Override
			public void putBitmap(String url, Bitmap bitmap)
			{
				// TODO Auto-generated method stub
				mLruCache.put(url, bitmap);
			}

		});
	}
	
	public ImageLoader getImageLoader()
	{
		return mImgLoader;
	}
	
	public RequestQueue getRequestQueue()
	{
		return mQueue;
	}

	public static VolleyHelper obtain(Context cx)
	{
		if (instance == null)
		{
			instance = new VolleyHelper(cx);
		}
		return instance;
	}
}
